package com.fuhao.springbootactivemq.comsumer;

import com.fuhao.springbootactivemq.Model.User;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

/**
 * @author zhang
 * @title: QueueListener1
 */
@Component
public class QueueListener2 {

    @Transactional
    @JmsListener(destination = "queue1",containerFactory = "queueListenerFactory")
    public void receiveQueue(Message message, Session session)
            throws JMSException {
        System.out.println("目的地：queue1，消费者2进来消费");
        try {
            ObjectMessage objectMessage = (ObjectMessage) message;
            User user = (User) objectMessage.getObject();
            System.out.println(user.toString());
            int i = 1/0;//测试重试机制是否成功
            message.acknowledge();// 使用手动签收模式，需要手动的调用，如果不在catch中调用session.recover()消息只会在重启服务后重发
        } catch (Exception e) {
            e.printStackTrace();
            session.recover();// 此不可省略 重发信息使用
        }
    }
}
